package org.example.examfx02.controller;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import org.example.examfx02.HelloApplication;
import org.example.examfx02.entity.*;
import org.example.examfx02.tool.SystemData;

import java.util.HashSet;
import java.util.List;

public class ChooseCourseViewController {
    SystemData sys = SystemData.getInstance();
    CourseSelectManager csManager = sys.getSc();
    List<Course> list = sys.getCourseChoose();
    Student curUser = (Student) SystemData.getCurUser();
    public static List<Course> hasChooseList = null;
    @FXML
    Button chooseBtn;
    @FXML
    Button dropBtn;
    @FXML
    TableView<Course> table;
    @FXML
    TableColumn<Course,String> courseId;
    @FXML
    TableColumn<Course,String> courseName;
    @FXML
    TableColumn<Course,Integer> courseNum;
    @FXML
    TableColumn<Course,String> courseTeacher;
    @FXML
    TableColumn<Course,Integer> courseMaxNum;
    @FXML
    TableColumn<Course,String> state;

    public void initialize(){
        hasChooseList = sys.getCourseChooseByStudentId(curUser.getId());
        courseId.setCellValueFactory(new PropertyValueFactory<>("id"));
        courseName.setCellValueFactory(new PropertyValueFactory<>("name"));
        courseNum.setCellValueFactory(new PropertyValueFactory<>("chooseNum"));
        courseTeacher.setCellFactory(column ->new TeacherNameCell());
        courseMaxNum.setCellFactory(column -> new MaxNumCell());
        state.setCellFactory(column->new HasChooseCell());
        // 为公共列设置居中显示
        courseId.setCellFactory(MainViewController.createCenteredCellFactory());
        courseName.setCellFactory(MainViewController.createCenteredCellFactory());
        courseNum.setCellFactory(MainViewController.createCenteredCellFactory());
        table.setItems(FXCollections.observableArrayList(list));
    }
    public void update(){
        hasChooseList = sys.getCourseChooseByStudentId(curUser.getId());
        table.refresh();
    }
    public void choose(){
        Course course = table.getSelectionModel().getSelectedItem();
        int index = table.getSelectionModel().getSelectedIndex();
        String hasChoose = state.getCellData(index);
        String id = courseId.getCellData(index);
        System.out.println(hasChoose +" "+ id);
        if(course!=null){
            if(hasChooseList.contains(course)){
                System.out.println("XXX");
                HelloApplication.showAlert("警告","无法选择已选课程");
            }else{
                // 选课
                boolean ok = csManager.add(new SelectCourse(curUser.getId(),id));
                if(!ok){
                    HelloApplication.showAlert("警告","课程已满");
                }
            }
        }else{
            HelloApplication.showAlert("警告","请先选择课程！！！");
        }
        // 更新
        update();
    }
    public void drop(){
        Course course = table.getSelectionModel().getSelectedItem();
        int index = table.getSelectionModel().getSelectedIndex();
        String hasChoose = state.getCellData(index);
        String id = courseId.getCellData(index);
        if(course!=null){
            if(!hasChooseList.contains(course)){
                HelloApplication.showAlert("警告","无法退掉未选课程");
            }else{
                Alert alert = HelloApplication.showConfirmAlert("退课","确认要退课吗？");
                if(alert.getResult() == ButtonType.OK){
                    csManager.remove(new SelectCourse(curUser.getId(),id));
                }
            }

        }else{
            HelloApplication.showAlert("警告","请先选择课程！！！");
        }
        // 更新
        update();
    }
}
